package core1;


//an abstract physical object 
public class PhysicalObject implements Physable {
	
	private final float DEFAULT_XPOS = 0f ;
	private final float DEFAULT_YPOS = 0f ;
	private final float DEFAULT_MASS = 10f ;
	private final float DEFAULT_K = 0.2f ;
	private final float DEFAULT_DAMP = 0.9f ;
	
	
	
	public PhysicalObject(float xpos, float ypos, float mass, float k,
			float damp) {
		super();
		this.xpos = targetxpos = xpos;
		this.ypos = targetypos = ypos;
		this.mass = mass;
		this.k = k;
		this.damp = damp;
	}


	public PhysicalObject() {
		
		super();
		this.xpos = targetxpos = DEFAULT_XPOS ;
		this.ypos = targetypos = DEFAULT_YPOS ;
		this.mass = DEFAULT_MASS;
		this.k = DEFAULT_K ;
		this.damp = DEFAULT_DAMP ;
		
	}





	public float getXpos() {
		return xpos;
	}


	public float getYpos() {
		return ypos;
	}
	
	private boolean moving = true;
	
	private float tolerance = 0.01f;

	private float xpos, ypos;
	private float targetxpos, targetypos;

	private float mass = 10;       // Mass 
	private float k = 0.02f;    // Spring constant 
	private float damp = 0.95f;       // Damping 


	private float velx = 0.0f;   // X Velocity 
	private float vely = 0.0f;   // Y Velocity 
	private float accel = 0;    // Acceleration 
	private float force = 0;    // Force 

	public void setTarget(float x, float y)
	{
		targetxpos  = x;
		targetypos  = y;
		moving = true;
	}


	public void update()
	{
	if (moving)

	{
		force = -k * (ypos - targetypos);  // f=-ky 
		accel = force / mass;                 // Set the acceleration, f=ma == a=f/m 
		vely = damp * (vely + accel);         // Set the velocity 
		ypos = ypos + vely;           // Updated position 

		force = -k * (xpos - targetxpos);  // f=-ky 
		accel = force / mass;                 // Set the acceleration, f=ma == a=f/m 
		velx = damp * (velx + accel);         // Set the velocity 
		xpos = xpos + velx;           // Updated position
		
		//  stop updating if not moving
		if (Math.abs(velx)+Math.abs(vely) < tolerance & Math.abs(xpos-targetxpos)+ Math.abs(ypos-targetypos) < tolerance) 
			{moving = false;};
	}
	}

	@Override
	public void setPhysics(float mass, float k, float damp) {
		this.mass = mass;
		this.k = k;
		this.damp = damp; 
		
		
	}






	
	
	
}
